package com.qian.apiserver.service.serviceImpl;

import com.qian.apiserver.dao.UserDao;
import com.qian.apiserver.dao.WithdrawalDao;
import com.qian.apiserver.entity.GmUser;
import com.qian.apiserver.entity.GmWithdrawal;
import com.qian.apiserver.model.User;
import com.qian.apiserver.model.Withdrawal;
import com.qian.apiserver.model.WithdrawalModel;
import com.qian.apiserver.service.IUserService;
import com.qian.apiserver.service.WithdrawalServiceBase;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import com.yaowk.service.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import com.qian.apiserver.service.IWithdrawalService;

import javax.persistence.criteria.*;
import java.util.*;

@Service
public class WithdrawalServiceImpl extends WithdrawalServiceBase {

    @Autowired
    private WithdrawalDao dao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private IUserService userService;
    public Map find(PageReq pageReq, final WithdrawalModel model) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "time"));
        Page<GmWithdrawal> page = dao.findAll(new Specification<GmWithdrawal>() {
            public Predicate toPredicate(Root<GmWithdrawal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Date> timePath = root.get("time");
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if(isNotEmpty(model.getUserId()))
                    condition = cb.and(condition,cb.equal(root.<Integer>get("userId"),model.getUserId()));
                if(isNotEmpty(model.getStatus()))
                    condition = cb.and(condition,cb.equal(root.<Byte>get("status"),model.getStatus()));
                if(isNotEmpty(model.getWay()))
                    condition = cb.and(condition,cb.equal(root.<Byte>get("way"),model.getWay()));
                if (isNotEmpty(model.getBeginDate())&& isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.between(timePath,model.getBeginDate(),model.getEndDate()));
                else if (isNotEmpty(model.getBeginDate()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(timePath,model.getBeginDate()));
                else if (isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.lessThanOrEqualTo(timePath,model.getEndDate()));
                return condition;
            }
        },pageRequest);
        PageResp<Withdrawal> pageResp = page2pageResp(page);
        List<WithdrawalModel> list = new ArrayList<WithdrawalModel>();
        for(Withdrawal withdrawal:pageResp.getList()) {
            WithdrawalModel model1 = new WithdrawalModel();
            User user = userService.findById(withdrawal.getUserId());
            model1.setUserId(user.getId());
            model1.setUserName(user.getUsername());
            model1.setUserCode(user.getUserCode());
            model1.setMoney(withdrawal.getMoney());
            model1.setTime(withdrawal.getTime());
            model1.setWay(withdrawal.getWay());
            model1.setPhone(withdrawal.getPhone());
            model1.setCardholder(withdrawal.getCardholder());
            model1.setWithdrawalNumber(withdrawal.getWithdrawalNumber());
            model1.setStatus(withdrawal.getStatus());
            list.add(model1);
        }
        Map map = new LinkedHashMap();
        map.put("totalPage",pageResp.getTotalPage());
        map.put("totalRow",pageResp.getTotalRow());
        map.put("pageSize",pageResp.getPageSize());
        map.put("pageNumber",pageResp.getPageNumber());
        map.put("list",list);
        return map;
    }

    public List findName(PageReq pageReq, final WithdrawalModel model) {
        PageRequest pageRequest = new PageRequest(pageReq.getPageNumber() - 1, pageReq.getPageSize(), new Sort(Sort.Direction.DESC, "time"));
        Page<GmWithdrawal> page = dao.findAll(new Specification<GmWithdrawal>() {
            public Predicate toPredicate(Root<GmWithdrawal> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<Date> timePath = root.get("time");
                Predicate condition = cb.isNotNull(root.<Integer>get("id"));
                if(isNotEmpty(model.getUserId()))
                    condition = cb.and(condition,cb.equal(root.<Integer>get("userId"),model.getUserId()));
                if(isNotEmpty(model.getStatus()))
                    condition = cb.and(condition,cb.equal(root.<Byte>get("status"),model.getStatus()));
                if(isNotEmpty(model.getWay()))
                    condition = cb.and(condition,cb.equal(root.<Byte>get("way"),model.getWay()));
                if (isNotEmpty(model.getBeginDate())&& isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.between(timePath,model.getBeginDate(),model.getEndDate()));
                else if (isNotEmpty(model.getBeginDate()))
                    condition = cb.and(condition,cb.greaterThanOrEqualTo(timePath,model.getBeginDate()));
                else if (isNotEmpty(model.getEndDate()))
                    condition = cb.and(condition,cb.lessThanOrEqualTo(timePath,model.getEndDate()));
                return condition;
            }
        },pageRequest);
        PageResp<Withdrawal> pageResp = page2pageResp(page);
        List<WithdrawalModel> list = new ArrayList<WithdrawalModel>();
        for(Withdrawal withdrawal:pageResp.getList()) {
            WithdrawalModel model1 = new WithdrawalModel();
            User user = userService.findById(withdrawal.getUserId());
            model1.setUserId(user.getId());
            model1.setUserName(user.getUsername());
            model1.setUserCode(user.getUserCode());
            model1.setMoney(withdrawal.getMoney());
            model1.setTime(withdrawal.getTime());
            model1.setWay(withdrawal.getWay());
            model1.setPhone(withdrawal.getPhone());
            model1.setCardholder(withdrawal.getCardholder());
            model1.setWithdrawalNumber(withdrawal.getWithdrawalNumber());
            model1.setStatus(withdrawal.getStatus());
            list.add(model1);
        }
        return list;
    }
}